<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>部门管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
		<!--查询代码  开始-->
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="searchFrm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">关键字</label>
                            <div class="layui-input-inline">
                                <input type="text" name="keywords" id="keywords" placeholder="请输入关键字" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn" lay-submit  lay-filter="data-search-btn"><i class="layui-icon">&#xe615;</i> 搜 索</button>
                            <button type="button" class="layui-btn layui-btn-warm" onclick="javascript:$('#searchFrm')[0].reset();tableIns.clearFilter();" ><i class="layui-icon">&#xe669;</i> 清 空</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
		<!--查询结束  结束-->
		
		
	    <!--数据表格  开始-->
		
        <div style="margin-top: 0.625rem;">
			<button type="button" class="layui-btn layui-btn-sm"  id="data-add-btn">添加部门</button>
		</div>
        <table class="layui-hide" id="deptTableId" lay-filter="deptTableFilter"></table>


        <script type="text/html" id="deptTableTemplet">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
		
		<script type="text/html" id="availableTpl">
		  <!-- 这里的 checked 的状态只是演示 -->
		  <input type="checkbox" name="available" value="{{d.id}}" lay-skin="switch" lay-text="可用|不可用" lay-filter="availableFilter" {{ d.available == 1 ? 'checked' : '' }}>
		</script>
		<script type="text/html" id="spreadTpl">
		  <!-- 这里的 checked 的状态只是演示 -->
		  <input type="checkbox" name="spread" value="{{d.id}}" lay-skin="switch" lay-text="展开|不展开" lay-filter="spreadFilter" {{ d.spread == 1 ? 'checked' : '' }}>
		</script>
		
	    <!--数据表格  结束-->
	
		<!--添加和修改弹出层代码  开始-->
		<div id="addAndUpdateDiv" style="display: none;padding: 0.3125rem;">
			<form id="dataFrm" lay-filter="dataFrm" method="post" class="layui-form layui-form-pane">
				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">父级部门</label>
						<div class="layui-input-inline">
							<input type="hidden" name="id" />
							<input type="hidden" name="pid" id="pid" />
							<input type="text" readonly="readonly" id="parentdeptname" lay-verify="" autocomplete="off" placeholder="请选择父级部门" class="layui-input">
						</div>
					</div>
					<div class="layui-inline">
						<button type="button" class="layui-btn layui-btn-warm" id="openDeptSelectLayer"><i class="layui-icon">&#xe608;</i> 选择</button>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">部门名称</label>
					<div class="layui-input-block">
						<input type="text" name="title" lay-verify="" autocomplete="off" placeholder="请输入部门名称" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">部门地址</label>
					<div class="layui-input-block">
						<input type="text" name="address"  autocomplete="off" placeholder="请输入部门地址" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">部门备注</label>
					<div class="layui-input-block">
						<textarea placeholder="请输入部门备注" name="remark" class="layui-textarea"></textarea>
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">创建时间</label>
						<div class="layui-input-inline">
							<input type="text" name="createtime" id="createtime" readonly="readonly" lay-verify="" autocomplete="off" placeholder="请选择创建时间" class="layui-input">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">排序码</label>
						<div class="layui-input-inline">
							<input type="number" name="ordernum" id="ordernum" lay-verify="" autocomplete="off" placeholder="请输入排序码" class="layui-input">
						</div>
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block" style="text-align: center;">
						 <button type="button" class="layui-btn" lay-submit  lay-filter="doSubmit"><i class="layui-icon">&#xe608;</i> 添 加</button>
						 <button type="button" class="layui-btn layui-btn-warm" id="doClose" ><i class="layui-icon">&#x1007;</i> 关 闭</button>
					</div>
				</div>
			</form>
			
			
		</div>
		
		
		<!--添加和修改弹出层代码  结束-->
    </div>
</div>
<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../lib/layui/layui.js" charset="utf-8"></script>
<script src="../../../js/lay-config.js?v=2.0.0" charset="utf-8"></script>
<script src="../../../lib/jquery-3.4.1/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../../lib/common/common.js" charset="utf-8"></script>
<script src="../../../lib/common/app_select_layer.js" charset="utf-8"></script>

<script>
	var tableIns;
    layui.use(['form', 'table','treeTable','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
			laydate=layui.laydate,
			treeTable=layui.treeTable;
			
		laydate.render({
			elem:'#createtime',
			type:'datetime'
		});
		
        // 渲染表格
        tableIns = treeTable.render({
            elem: '#deptTableId',
            tree: {
                iconIndex: 1,
				isPidData:true,
				openName:'spread'
            },
            cols: [
                {type: 'numbers'},
               /* {type: 'checkbox'}, */
                {field: 'title', title: '部门名称', width: 200},
				{field: 'id', title: '部门ID', width: 80,align:'center'},
				{field: 'pid', title: '部门PID', width: 90,align:'center'},
				{field: 'address', title: '部门地址', width: 120,align:'center'},
				{field: 'remark', title: '部门备注', width: 150,align:'center'},
				{field: 'spread', title: '是否展开', width: 100,align:'center', templet: '#spreadTpl',},
				{field: 'available', title: '是否可用', width: 100,align:'center', templet: '#availableTpl',},
				{field: 'ordernum', title: '排序码', width: 80,align:'center'},
				{field: 'createtime', title: '创建时间', width: 180,align:'center'},
                {align: 'center', toolbar: '#deptTableTemplet', title: '操作', width: 150}
            ],
			reqData: function(data, callback) {
				$.get(api+"dept/loadAllDept", function (res) {
					callback(res.data);
				});
			}
        });
		// 监听搜索操作
		form.on('submit(data-search-btn)', function (data) {
		    tableIns.filterData(data.field.keywords);  
		    return false;
		});
		
		//监听添加按钮的事件
		$("#data-add-btn").click(function(){
			openAddLayer();
		});
		
		
		treeTable.on('tool(deptTableId)', function (obj) {
		    var data = obj.data;
		    if (obj.event === 'edit') {
				openUpdateLayer(obj);
		    } else if (obj.event === 'delete') {
		       deleteDataById(obj);
		    }
		});
		
		var mainIndex;
		var url;
		//打开添加页面
		function openAddLayer(){
			 mainIndex = layer.open({
			    title: '添加部门',
			    type: 1,  
			    shade: 0.2,
			    maxmin:true,
			    shadeClose: true,
			    area: ['1000px', '600px'],
			    content: $("#addAndUpdateDiv"),
				success:function(){
					url=api+"dept/addDept";
					$("#dataFrm")[0].reset();//重置表单
					
					//请求最大的排序码
					$.get(api+"dept/loadDeptMaxOrderNum",function(res){
						$("#ordernum").val(res.data);
					})
					
				}
			});
			$(window).on("resize", function () {
			    layer.full(mainIndex);
			});
		}
		//打开修改的弹出层
		function openUpdateLayer(obj){
			var data = obj.data;
			 mainIndex = layer.open({
			    title: '编辑部门',
			    type: 1,
			    shade: 0.2,
			    maxmin:true,
			    shadeClose: true,
			    area: ['1000px', '600px'],
			    content: $("#addAndUpdateDiv"),
				success:function(){
					url=api+"dept/updateDept";
					console.log(data);
					form.val("dataFrm",data);//填充表单
					var pid=data.pid;
					//根据PID查询部门信息
					$.get(api+"/dept/queryDeptById",{id:pid},function(res){
						$("#parentdeptname").val(res.data.title);
					});
					
				}
			});
			$(window).on("resize", function () {
			    layer.full(mainIndex);
			});
		}
		
		//提交数据
		form.on("submit(doSubmit)",function(data){
			var params= $("#dataFrm").serialize();
			$.post(url,params,function(res){
				console.log(res);
				layer.msg(res.msg);
				if(res.code==1){
					tableIns.refresh();
				}
				layer.close(mainIndex);
			});
			
			return false;
		});
		
		//打开选择父级部门的选择框
		$("#openDeptSelectLayer").click(function(){
			openDeptSelectLayer($("#pid"),$("#parentdeptname"))
		});
		
		 //监听展开操作
		  form.on('switch(spreadFilter)', function(obj){
		    //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
			$.post(api+"dept/updateDept",{id:this.value,spread:obj.elem.checked?1:0},function(res){
				layer.msg(res.msg);
			})
		  });
		  
		  //监听可用操作     
		  form.on('switch(availableFilter)', function(obj){
		    //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
			$.post(api+"dept/updateDept",{id:this.value,available:obj.elem.checked?1:0},function(res){
				layer.msg(res.msg);
			})
		  });
		  
		  //根据ID删除
		  function deleteDataById(obj){
		  	 var data = obj.data;
		  	layer.confirm('真的删除【'+data.title+'】这个部门么', function (index) {
		  		$.post(api+"dept/deleteDept",{id:data.id},function(res){
		  			if(res.code==1){
		  				tableIns.refresh();
		  			} 
		  			layer.msg(res.msg);
		  			layer.close(index);
		  		})
		  	});
		  }
		  
    });
</script>
<script>

</script>

</body>
</html>